//
//  AboutUsViewController.m
//  myhuibao
//
//  Created by li jun on 13-4-3.
//  Copyright (c) 2013年 myhuibao. All rights reserved.
//

#import "AboutUsViewController.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "SBJson.h"
#import "OMGToast.h"
#import "RequestLoadingView.h"
@interface AboutUsViewController ()

@end

@implementation AboutUsViewController
@synthesize wvMain;
@synthesize lbVersion;
@synthesize svMain;
@synthesize req;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    lbVersion.text = [NSString stringWithFormat:@"版本号：%@",curversion ];
//    svMain.pagingEnabled = YES;
//    svMain.clipsToBounds = NO;
//    svMain.contentSize = CGSizeMake(svMain.frame.size.width, svMain.frame.size.height*2);
    svMain.showsHorizontalScrollIndicator = NO;
//    svMain.showsVerticalScrollIndicator = NO;
    //    svMain.scrollsToTop = NO;
    //svMain.delegate = self;
    //    [svMain setContentSize:CGSizeMake(320*3, 920)];
    wvMain.delegate = self;
    [svMain setContentOffset:CGPointMake(0, 0)];
    svMain.frame = CGRectMake(0,36,320,svMain.frame.size.height);

    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"image_background.png"]];
    svMain.backgroundColor = background; 
    [self getSearchResult];
    
}
-(void) viewWillDisappear:(BOOL)animated{
    if (!isReqFinished){
        [req clearDelegatesAndCancel];
        [req cancel];
    }
}
- (void) getSearchResult{
    //异步请求
    NSURL *url = [NSURL URLWithString:url_aboutUs];
    req = [ASIFormDataRequest requestWithURL:url];
    [req setDidFinishSelector:@selector(finishReq:)];
    isReqFinished = false;
    [req setDelegate:self];
    
    [req startAsynchronous];


}
//下面是两个代理函数,当数据请求成功时会调用requestFinished，请求失败时（如网络问题或服务器内部错误）会调用requestFailed
- (void)finishReq:(ASIHTTPRequest *)request
{
    // Use when fetching text data
    isReqFinished = true;
    NSString *jsonStr = [request responseString];
    NSDictionary *dic = [jsonStr JSONValue];
    if ([dic objectForKey:@"data"] != [NSNull null]) {
        
    [wvMain loadHTMLString:[dic objectForKey:@"data"]
                      baseURL:nil];
    }
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
    isReqFinished = true;
    NSError *error = [request error];
    NSLog(@"%@",error);
    if (error.code==net_failure_code) {
        [OMGToast showWithText:net_failure_msg bottomOffset:100];
    }
}


- (void)viewDidUnload
{
    [self setWvMain:nil];
    [self setSvMain:nil];
    [self setLbVersion:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)doBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    CGFloat webViewHeight = 0.0f;
    if (webView.subviews.count > 0)
    {
        UIView *scrollerView = [webView.subviews objectAtIndex:0];//为什么要取第一个？
        if (scrollerView.subviews.count > 0)
        {
            UIView *webDocView = scrollerView.subviews.lastObject;
            if ([webDocView isKindOfClass:[NSClassFromString(@"UIWebDocumentView") class]])
            {
                webViewHeight = webDocView.frame.size.height;//获取文档的高度
                webView.frame= CGRectMake(0,80,webDocView.frame.size.width,webDocView.frame.size.height); //更新UIWebView 的高度
                svMain.contentSize=CGSizeMake(svMain.frame.size.width, 100+webViewHeight);//调整底层的contensize 的大小
            }
        }
    }
}
@end
